<?php


namespace app\admin\common;


class Selector
{
    public static $data;
    public static $checked;
    public static $name;
    public static $class;
    /**
     * Selector constructor.
     * @param array $list []
     */
    public static function render($data,$name,$checked='',$class='')
    {
        self::$data = $data;
        self::$name = $name;
        self::$checked = $checked;
        self::$class = $class;
        return sprintf(
            '<select class="%s form-control" id="example-select" name="%s" size="1">%s </select>',
            self::$class,
            self::$name,
            self::body()
        );

    }

    public static function body(){
        $html = '<option value="">请选择</option>';
        if(self::$data && is_array(self::$data)){
            foreach (self::$data  as $key => $val){
                if(self::$checked && self::$checked == $key){
                    $html.= "<option value=\"{$key}\" selected>{$val}</option>";
                }else{
                    $html.= "<option value=\"{$key}\" >{$val}</option>";
                }
            }
        }
        return $html;
    }

}